<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="500px" append-to-body custom-class="import-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="打扫人" prop="cleanerId">
          <el-select v-model="form.cleanerId" placeholder="请选择" style="width: 240px" filterable clearable>
            <el-option v-for="item in cleanerList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { debounce } from "@/utils";
import { getCleanerUserInfo, assignSweep } from "@/api/houseCleaning/sweep";
export default {
  components: {

  },
  props: {
    title: {
      default: "人员指派",
    },
    value: {
      default: false,
    },
    data: {
      default: undefined,
    },
  },
  data() {
    return {
      visible: false,
      form: {},
      rules: {
        cleanerId: [
          { required: true, message: '请选择打扫人', trigger: 'blur' }
        ],
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: [],
      cleanerList: [],
    };
  },
  watch: {
    value(newVal, oldVal) {
      if (newVal) {
        this.init();
      }
      this.visible = newVal; // 监测value的变化，并赋值。
    },
    visible(val) {
      this.$emit("input", val); // 此处监测visible目的为关闭弹窗时，重新更换value值，注意emit的事件一定要为input。
    },
  },
  methods: {
    init() {
      this.reset()
      this.getCleanerUserInfo()
    },
    reset() {
      this.form = {
        cleanerId: '',
        houseSweepId: this.data.houseSweepId
      }
      this.resetForm("form");
    },
    getCleanerUserInfo(){
      let params = {
        houseId: this.data.houseId,
        cleanerType:2
      }
      getCleanerUserInfo(params).then(res => {
        this.cleanerList = res.data.map(item => {
          return {
            label: item.cleanerName,
            value: item.cleanerId
          }
        })
      })
    },
    submitForm: debounce(function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          assignSweep(this.form).then(response => {
            if (response.code == 200) {
              this.$emit("success");
              this.$message.success(response.msg);
              this.visible = false;
            } else {
              this.$message.error(response.msg);
            }
          })
        }
      })
    }, 500),
    cancel() {
      this.reset();
      this.visible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/dialog.scss";
</style>
